home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4435 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.5 KB  |  69 lines

  1. Path: cs.ruu.nl!usenet
  2. From: piet@stego.cs.ruu.nl (Piet van Oostrum)
  3. Newsgroups: comp.lang.c,comp.lang.c++,gnu.gcc.help,gnu.g++.help,comp.os.msdos.djgpp
  4. Subject: Re: float != float and floats as return types
  5. Date: 30 Jan 1996 10:35:12 +0100
  6. Organization: Universiteit Utrecht, Dept. of Computer Science
  7. Sender: piet@stego.cs.ruu.nl
  8. Message-ID: <wz3f8yt48f.fsf@stego.cs.ruu.nl>
  9. References: <4ej9lb$mpc@fu-berlin.de>
  10. NNTP-Posting-Host: stego.cs.ruu.nl
  11. In-reply-to: axl@zedat.fu-berlin.de's message of Mon, 29 Jan 1996 20:12:38 GMT
  12. X-Newsreader: Gnus v5.0.8
  13.  
  14. >>>>> axl@zedat.fu-berlin.de (Axel Thimm) (AT) writes:
  15.  
  16. AT> Hello,
  17. AT> I am getting confused, about how C/C++ manage float binary operations,
  18. AT> in particular multiplication. The next C++ example gives me surprising
  19. AT> results:
  20. AT>     *** cut here: begin file t_prec.cc
  21. AT>     #include <iostream.h>
  22. AT>     #include <iomanip.h>
  23. AT>     #include <math.h>
  24. AT>     float quad( float );
  25. AT>     int main() {
  26. AT>       for( int i=0; i<10; ++i ) {
  27. AT>         float a, b, c;
  28. AT>         a = i/13.123123;
  29. AT>         b = a*a;
  30. AT>         c = quad(a);
  31. AT>         cout << (b - c) << '\t';
  32. AT>         cout << (b - a*a) << '\t';
  33. AT>         cout << (c - quad(a)) << '\n';
  34. AT>       }
  35. AT>       return 0;
  36. AT>     }
  37. AT>     float quad( float x ) { return x*x; }
  38. AT>     *** cut here: end file t_prec.cc
  39. AT> (Sorry for not using C-syntax. You can substitute the "cout <<" part
  40. AT> with "printf" calls, I realized it to late to try it out, and
  41. AT> "translating" it on the fly might get typos in)
  42. AT> I compile with the MSDOS port of gcc as follows: gcc t_prec.cc -lgpp 
  43. AT> So here is my output:
  44. AT>     *** cut here: begin output
  45. AT>     0    0    0
  46. AT>     0    3.21547e-11    3.21547e-11
  47. AT>     0    1.28619e-10    1.28619e-10
  48. AT>     0    -1.25443e-09    -1.25443e-09
  49. AT>     0    5.14475e-10    5.14475e-10
  50. AT>     0    7.14357e-10    7.14357e-10
  51. AT>     0    -5.01772e-09    -5.01772e-09
  52. AT>     0    -1.46904e-08    -1.46904e-08
  53. AT>     0    2.0579e-09    2.0579e-09
  54. AT>     0    -2.02694e-09    -2.02694e-09
  55. AT>     *** cut here: end output
  56. AT> At first I thought all numbers should be zeros. The middle column might
  57. AT> not be zero, if the compiler calculates in higher precision than what
  58. AT> the variables are, so an intermediate storage (b) might loose some
  59. AT> digits, but I cannot understand what happens with c! Both times a
  60. AT> function is called, that _is_not_ inlined, so in both cases the same
  61. AT> loss of digits should be observed.
  62.  
  63. Gcc sometimes does inline function calls even if you don't say so.
  64. Especially with simple functions. Maybe -O0 or -fno-default-inline
  65. might help.
  66. -- 
  67. Piet van Oostrum <piet@cs.ruu.nl>
  68. http://www.cs.ruu.nl/~piet
  69.